<?
class Settings extends CI_Model
{
	public $data = null;

	function __construct()
	{
		parent::__construct();
		$this->get();
	}

	function getTitle()
	{
		return $this->data->title;
	}

	function get()
	{
		$res = $this->db->select('pKey, pVal')->get('settings')->result();
		$this->data = array();
		foreach ($res as $val)
			$this->data[$val->pKey] = $val->pVal;
		return $this->data = (object)$this->data;
	}

	function save($data = array())
	{
		$user = $this->user->id;
		$time = now();

		$this->load->helper('email_helper');

		if (isset($data['pVKAuth']))
		{
			$this->db->where('pKey', 'pVKAuth')->update('settings', array(
				'pVal' => 1,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pVKId')->update('settings', array(
				'pVal' => $data['pVKId'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pVKSecret')->update('settings', array(
				'pVal' => $data['pVKSecret'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		else
		{
			$this->db->where('pKey', 'pVKAuth')->update('settings', array(
				'pVal' => 0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		if (isset($data['pTwitterAuth']))
		{
			$this->db->where('pKey', 'pTwitterAuth')->update('settings', array(
				'pVal' => 1,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pTwitterId')->update('settings', array(
				'pVal' => $data['pTwitterId'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pTwitterSecret')->update('settings', array(
				'pVal' => $data['pTwitterSecret'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		else
		{
			$this->db->where('pKey', 'pTwitterAuth')->update('settings', array(
				'pVal' => 0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		if (isset($data['pFBAuth']))
		{
			$this->db->where('pKey', 'pFBAuth')->update('settings', array(
				'pVal' => 1,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pFBId')->update('settings', array(
				'pVal' => $data['pFBId'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pFBSecret')->update('settings', array(
				'pVal' => $data['pFBSecret'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		else
		{
			$this->db->where('pKey', 'pFBAuth')->update('settings', array(
				'pVal' => 0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		if (isset($data['pMailruAuth']))
		{
			$this->db->where('pKey', 'pMailruAuth')->update('settings', array(
				'pVal' => 1,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pMailruId')->update('settings', array(
				'pVal' => $data['pMailruId'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pMailruPrivate')->update('settings', array(
				'pVal' => $data['pMailruPrivate'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pMailruSecret')->update('settings', array(
				'pVal' => $data['pMailruSecret'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		else
		{
			$this->db->where('pKey', 'pMailruAuth')->update('settings', array(
				'pVal' => 0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		if (isset($data['pOKAuth']))
		{
			$this->db->where('pKey', 'pOKAuth')->update('settings', array(
				'pVal' => 1,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pOKId')->update('settings', array(
				'pVal' => $data['pOKId'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pOKPublic')->update('settings', array(
				'pVal' => $data['pOKPublic'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
			$this->db->where('pKey', 'pOKSecret')->update('settings', array(
				'pVal' => $data['pOKSecret'],
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		else
		{
			$this->db->where('pKey', 'pOKAuth')->update('settings', array(
				'pVal' => 0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		// OAuth end

		if (isset($data['siteTitle']))
		{
			$this->db->where('pKey', 'title')->update('settings', array(
				'pVal' => htmlEscape($data['siteTitle']),
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		if (isset($data['blogSize']))
		{
			$this->db->where('pKey', 'blogSize')->update('settings', array(
				'pVal' => abs($data['blogSize'])?abs($data['blogSize']):3,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		if (isset($data['sessExpiration']))
		{
			$this->db->where('pKey', 'sessExpiration')->update('settings', array(
				'pVal' => abs($data['sessExpiration'])?abs($data['sessExpiration']):7200,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		if (isset($data['pFeedbackEmail']))
		{
			$this->db->where('pKey', 'pFeedbackEmail')->update('settings', array(
				'pVal' => valid_email($data['pFeedbackEmail'])?($data['pFeedbackEmail']):'',
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		
		if (isset($data['mailSentCount']))
		{
			$this->db->where('pKey', 'mailSentCount')->update('settings', array(
				'pVal' => abs($data['mailSentCount'])?abs($data['mailSentCount']):0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}
		if (isset($data['mailSentPeriod']))
		{
			$this->db->where('pKey', 'mailSentPeriod')->update('settings', array(
				'pVal' => abs($data['mailSentPeriod'])?abs($data['mailSentPeriod']):0,
				'tUserUpdated' => $user,
				'dUpdated' => $time
			));
		}

		// checkboxes
		$this->db->where('pKey', 'considerVisits')->update('settings', array(
			'pVal' => isset($data['considerVisits'])?1:0,
			'tUserUpdated' => $user,
			'dUpdated' => $time
		));
		$this->db->where('pKey', 'authorInTitle')->update('settings', array(
			'pVal' => isset($data['authorInTitle'])?1:0,
			'tUserUpdated' => $user,
			'dUpdated' => $time
		));
		
		$this->db->where('pKey', 'mailSentLimited')->update('settings', array(
			'pVal' => isset($data['mailSentLimited'])?1:0,
			'tUserUpdated' => $user,
			'dUpdated' => $time
		));
		return true;
	}
}
?>
